import pandas as pd
import numpy as np
from joblib import load
from sklearn.tree import DecisionTreeClassifier

class ClassificationDiseases:
    def __init__(self, model_path):
        self.model = load(model_path)
    
    def predict(self, data):
        print("原始数据：", data)
        X_new = data.iloc[:, [1, 2, 3]].values.astype(np.float32)
        print("处理后的特征数据：", X_new)
        predictions = self.model.predict(X_new)
        probabilities = self.model.predict_proba(X_new)
        
        # 将概率转换为小数并保留17位小数
        formatted_probabilities = []
        for proba in probabilities:
            sum_probas = sum(proba)
            adjusted_probas = [p / sum_probas for p in proba]  # 归一化
            # 确保每个概率值保留17位小数
            formatted_probas = [round(p, 17) for p in adjusted_probas]
            # 确保总和为1
            total = sum(formatted_probas)
            if abs(total - 1.0) > 1e-17:
                # 调整最后一个值以确保总和为1
                formatted_probas[-1] = 1.0 - sum(formatted_probas[:-1])
            formatted_probabilities.append(formatted_probas)
        result = {
            "地中海贫血": formatted_probabilities[0][1],
            "正常": formatted_probabilities[0][0],
            "缺铁性贫血": formatted_probabilities[0][2],
        }
        print("Predictions:", predictions)
        print("Formatted Probabilities:", formatted_probabilities)
        return result